package com.cskaoyan.day24.Homework24;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server02 {
    public static void main(String[] args) throws IOException {
        ServerSocket socket = new ServerSocket(5678);
        Socket socket1 = socket.accept();
        System.out.println(socket1.getRemoteSocketAddress() + "已建立");
        InputStream in = socket1.getInputStream();
        FileOutputStream out = new FileOutputStream("abc.txt");
        byte[] buf = new byte[1024];
        int readCount;
        while ((readCount = in.read(buf)) != -1) {
//            out.write(buf, 0, readCount);
            String msg = new String(buf, 0, readCount);
            if ("end".equals(msg)) {
                out.write(buf,0,readCount-"end".length());
                break;
            }
            System.out.println(msg);
            out.write(buf,0,readCount);
        }
        OutputStream out2 = socket1.getOutputStream();
        out2.write("FIN".getBytes());
        socket.close();
    }
}

